﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace ReaderControl
{
    using WP7Contrib.View.Controls.Reader;

    public partial class MainPage : PhoneApplicationPage
    {
        private int landscapewidth = 656;

        private int landscapeheight = 480;

        private int portraitwidth = 480;

        private int portraitheight = 696;

        // Constructor
        public MainPage()
        {
            this.Loaded += delegate(object sender, RoutedEventArgs args)
                {  };

            InitializeComponent();
        }

        private void PhoneApplicationPageOrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            if ((e.Orientation == PageOrientation.Landscape) || (e.Orientation == PageOrientation.LandscapeLeft) || (e.Orientation == PageOrientation.LandscapeRight))
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        this.ReaderControl.Width = landscapewidth;
                        this.ReaderControl.Height = landscapeheight;

                        foreach (ReaderItem item in this.ReaderControl.Items)
                        {
                            item.Width = landscapewidth;
                            item.Height = landscapeheight;
                        }
                    });
                
            }

            if ((e.Orientation == PageOrientation.Portrait) || (e.Orientation == PageOrientation.PortraitDown) || (e.Orientation == PageOrientation.PortraitUp))
            {
                Deployment.Current.Dispatcher.BeginInvoke(
                    () =>
                        {
                            this.ReaderControl.Height = portraitheight;
                            this.ReaderControl.Width = portraitwidth;

                            foreach (ReaderItem item in this.ReaderControl.Items)
                            {
                                item.Width = portraitwidth;
                                item.Height = portraitheight;
                            }
                        });
            }

            Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    if (this.ReaderControl.ScrollView != null)
                    {
                        this.ReaderControl.ScrollView.ScrollTo(this.ReaderControl.SelectedIndex);
                    }
                });
        }

        private void FontSizeMediumRadioButtonChecked(object sender, RoutedEventArgs e)
        {
           this.ReaderControl.ReaderItemsFontSize = 17;
        }

        private void FontSizeLargeRadioButtonChecked(object sender, RoutedEventArgs e)
        {
            if (this.ReaderControl == null)
            {
                return;
            }

            this.ReaderControl.ReaderItemsFontSize = 24;
        }

        private void FontSizeExtraLargeRadioButtonChecked(object sender, RoutedEventArgs e)
        {
            if (this.ReaderControl == null)
            {
                return;
            }
                        this.ReaderControl.ReaderItemsFontSize = 32;
        }

        private void FontFamilyLightRadioButtonChecked(object sender, RoutedEventArgs e)
        {
            if (this.ReaderControl == null)
            {
                return;
            }
                        this.ReaderControl.ReaderItemsFontFamily = new FontFamily("Segoe WP SemiLight");
        }

        private void FontFamilyBoldRadioButtonChecked(object sender, RoutedEventArgs e)
        {
            if (this.ReaderControl == null)
            {
                return;
            }
                        this.ReaderControl.ReaderItemsFontFamily = new FontFamily("Segoe WP Semibold");
        }

        private void FontFamilyNormalRadioButtonChecked(object sender, RoutedEventArgs e)
        {
            if (this.ReaderControl == null)
            {
                return;
            }
                        this.ReaderControl.ReaderItemsFontFamily = new FontFamily("Segoe WP");
        }

        private void PagingApplicationBarIconButtonClick(object sender, EventArgs e)
        {
            if (this.PageNavigation.Visibility == Visibility.Collapsed)
            {
                this.ShowPageNavigationStoryboard.Begin();
                return;
            }

            this.HidePageNavigationStoryboard.Begin();
        }

        private void FontApplicationBarIconButtonClick(object sender, EventArgs e)
        {
            if (this.FontSetting.Visibility == Visibility.Collapsed)
            {
                this.ShowFontSettingsStoryboard.Begin();
                return;
            }

            this.HideFontSettingsStoryboard.Begin();
        }
    }
}